package com.seeyon.apps.ebs.rcyh.controller;

import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.ebs.rcyh.manager.RcyhManager;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.controller.BaseController;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

public class RcyhController extends BaseController {
    private static Log log = LogFactory.getLog(RcyhController.class);
    private static RcyhManager rcyhManager = (RcyhManager) AppContext.getBean("rcyhManager");

    /**
    * 功能:更新日程中间数据库表
    * @Date: 2025/4/16
    */
    public void updateRcMiddleTable(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        try (PrintWriter out = response.getWriter()){
            String year = request.getParameter("year");
            rcyhManager = (RcyhManager) AppContext.getBean("rcyhManager");
            JSONObject dataParam = new JSONObject();
            dataParam.put("year",year);
            JSONObject jsonObject = rcyhManager.updateRcMiddleTable(dataParam);
            log.info("更新日程中间数据库表开始！！！");
        }catch (Exception e){
            log.error("更新日程中间数据库表异常！！！", e);
        }
    }

    /**
     * 功能:领导日程页面-右侧人员动迁信息
     * @Date: 2025/2/10
     */
    public void leadershipAgendaPerson(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        try (PrintWriter out = response.getWriter()){
            String year = request.getParameter("year");
            String userId = request.getParameter("userId");
            String state = request.getParameter("state");
            String page = request.getParameter("page");
            String limit = request.getParameter("limit");
            String deptId = request.getParameter("deptId");
            String name = request.getParameter("name");//模糊查询
            String sortType = request.getParameter("sortType");//排序类型 ：1：降序 2：升序 为空默认为初始排序
            String sortMonth = request.getParameter("sortMonth");//排序条件：1月：Jan ...... 年总数据：Total 为空则默认为总数据
            if (year != null && !year.isEmpty() && userId != null && !userId.isEmpty() && page != null && !page.isEmpty() && limit != null && !limit.isEmpty()){
                JSONObject rcJson = new JSONObject();
                rcJson.put("year", year);
                rcJson.put("userId", userId);
                rcJson.put("state", state);
                rcJson.put("page",page);
                rcJson.put("limit",limit);
                if (sortType != null && !sortType.isEmpty() && sortMonth != null && !sortMonth.isEmpty()){
                    rcJson.put("sortType",sortType);
                    rcJson.put("sortMonth",sortMonth);
                }/*else{
                    rcJson.put("sortType",1);
                    rcJson.put("sortMonth","Total");
                }*/
                if ((deptId == null || deptId.isEmpty()) && (name == null || name.isEmpty())){
                    JSONObject jsonObject = rcyhManager.leadershipAgendaPerson(rcJson);
                    out.write(jsonObject.toString());
                    //点击栏目默认
                }else if((deptId != null && !deptId.isEmpty()) && (name == null || name.isEmpty())){
                    rcJson.put("deptId",deptId);
                    JSONObject jsonObject = rcyhManager.leadershipAgendaPerson(rcJson);
                    out.write(jsonObject.toString());
                    //点击部门
                }else if(name != null && !name.isEmpty()){
                    rcJson.put("name",name);
                    //模糊查询
                    if (deptId != null && !deptId.isEmpty()){
                        rcJson.put("deptId",deptId);
                    }
                    JSONObject jsonObject = rcyhManager.leadershipAgendaPerson(rcJson);
                    out.write(jsonObject.toString());
                }
            }
        }catch (Exception e){
            log.error("领导日程异常！！！", e);
        }
    }

    public void personalSchedule(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        try (PrintWriter out = response.getWriter()){
            String year = request.getParameter("year");
            String userId = request.getParameter("userId");
            JSONObject rcJson = new JSONObject();
            if (year != null && !year.isEmpty() && userId != null && !userId.isEmpty()){
                rcJson.put("year",year);
                rcJson.put("userId",userId);
                JSONObject jsonObject = rcyhManager.personalSchedule(rcJson);
                out.write(jsonObject.toString());
                log.info("个人日程");
            }else{
                log.info("参数未接收到！！！");
            }
        }catch (Exception e){
            log.error("个人日程异常！！！", e);
        }
    }
}
